<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="学生姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 交付中心学员列表 -->
    <el-table v-loading="loading" :data="StudentList">
      <el-table-column label="交付中心名称" align="center" prop="payCenterName" width="100px" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="学管师" align="center" prop="learnName" />
      <el-table-column label="上课教练" align="center" prop="coachName" />
      <el-table-column label="课程名称" align="center" prop="className" />
      <el-table-column label="课程分类" align="center" prop="classTypeName" />
      <el-table-column label="总课时" align="center" prop="buyLearnTime" />
      <el-table-column label="交付学时" align="center" prop="payLearnTime" />
      <el-table-column label="已退学时" align="center" prop="retreatLearnTime" />
      <el-table-column label="剩余学时" align="center" prop="surplusLearnTime" />
      <el-table-column label="订单金额" align="center" prop="totalFee" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审批意见" align="center" prop="approvalRemark" />
      <el-table-column label="备注" align="center" prop="dropRemark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  v-if="scope.row.approvalStatus!='1'" @click="handleUpdate(scope.row)">退课</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 退课 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="125px">
        <el-form-item label="学员姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入学员姓名" disabled />
        </el-form-item>
        <el-form-item label="剩余学时" prop="surplusLearnTime" >
          <el-input v-model="form.surplusLearnTime" disabled style="width: 240px">
            <template #suffix>
              <span>时</span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="退款学时" prop="retreatLearnTime">
          <el-input-number v-model="form.retreatLearnTime"  @input="calculateRefundAmount" style="width: 240px">
            <template #suffix>
              <span>时</span>
            </template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="退款金额" prop="surplusAmount">
          <el-input v-model="form.surplusAmount"   style="width: 240px">
            <template #suffix>
              <span>元</span>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="dropRemark">
          <el-input v-model="form.dropRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listStudent,
  setLearnId,
  getStudent,
  delStudent,
  addStudent,
  updateStudent,
  updateTkupdateTk
} from '@/api/course/Student'

export default {
  name: "Student",
  dicts: ['course_yes_no', 'c_is_use', 'c_position', 'sys_user_sex', 'c_teach_method','c_order_status', 'c_approval_status'],
  props: {
    orderNo: {
      type: String
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        sex: null,
        level: null,
        phone: null,
        recPhone: null,
        storeUserId: null,
        storeCode: null,
        storeName: null,
        firstTime: null,
        isAddGroup: null,
        learnId: null,
        coachId: null,
        buyLearnTime: null,
        payLearnTime: null,
        retreatLearnTime: null,
        surplusLearnTime: null,
        className: null,
        classTypeId: null,
        classTypeName: null,
        expectTime: null,
        orderType: null,
        orderCode: null,
        payAmount: null,
        isPay: null,
        learnRemark: null,
        payGroupId: null,
        isTest: null,
        isNew: null,
        isUse: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      StudentList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学员信息列表 */
    getList() {
      this.loading = true;
      if(this.orderNo){
        this.queryParams.orderCode=this.orderNo;
      }
      // 获取学员信息列表
      listStudent(this.queryParams).then(response => {
        this.StudentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        sex: null,
        level: null,
        phone: null,
        recPhone: null,
        storeUserId: null,
        storeCode: null,
        storeName: null,
        firstTime: null,
        isAddGroup: null,
        learnId: null,
        coachId: null,
        buyLearnTime: null,
        payLearnTime: null,
        retreatLearnTime: null,
        surplusLearnTime: null,
        className: null,
        classTypeId: null,
        classTypeName: null,
        expectTime: null,
        orderType: null,
        orderCode: null,
        payAmount: null,
        surplusAmount: null,
        orderStatus: null,
        approvalStatus: null,
        dropRemark: null,
        isPay: null,
        learnRemark: null,
        payGroupId: null,
        isTest: null,
        isNew: null,
        isUse: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getStudent(row.id).then(response => {
        this.form = response.data;
        console.log(response.data);
        this.open = true;
        this.title = "添加退课信息";
      });
    },

    /**
     * 计算退款金额
     */
    calculateRefundAmount() {
      if(this.form.retreatLearnTime>this.form.surplusLearnTime){
        this.$modal.msgError("退课学时不能大于剩余学时");
        return;
      }
        const stje= (this.form.amount-this.form.hhrAmount)*this.form.retreatLearnTime;
         const  ytje= this.form.amount*this.form.retreatLearnTime;
        const  xcje=   this.form.hhrAmount*this.form.retreatLearnTime;
         this.form.surplusAmount=stje;
          const hhrName =  this.form.hhrName;
        // 自动填充备注
        this.form.dropRemark = "应退"+ytje+"元、实退"+stje+"元,销售"+hhrName+"应退"+xcje+"元";

    },

    /** 提交按钮 */
    submitForm() {
      this.form.approvalStatus = '1'; // 设置审核状态为待审核
      this.form.coachOrderStatus=""
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            // 退课课时要小于等于 剩余课时
            if(this.form.retreatLearnTime>this.form.surplusLearnTime){
              this.$modal.msgError("退课学时不能大于剩余学时");
              return;
            }
            updateTkupdateTk(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    calculateTime() {
      const starTime = this.classForm.starTime
      const endTime = this.classForm.endTime
      let diffHours = 0;
      if (starTime && endTime) {
        // 假设时间格式为 "HH:mm"
        const [sh, sm] = starTime.split(':').map(Number);
        const [eh, em] = endTime.split(':').map(Number);
        const start = sh * 60 + sm;
        const end = eh * 60 + em;
        diffHours = (end - start) / 60;
        // 保留一位小数
        diffHours = Math.round(diffHours * 10) / 10;
      }
      console.log('相差小时数:', diffHours);
      this.classForm.learnNum = diffHours; // 将学时转换为字符串
    },

  }
};
</script>
